/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  这个部分是Teris程序的主程序
 *
 *        Version:  1.0
 *        Created:  2008年12月09日 22时31分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
 *        Company:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */
#include "config.h"
#include <iostream>
#include <stdlib.h>
#ifdef USE_WIN32
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_gfxPrimitives_font.h>
#else
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_gfxPrimitives_font.h>
#endif
#include <wchar.h>
#include "config.h"
#include "map.h"
#include "teris.h"
using namespace std;

//消息定义
#define TIMEMSG 		0x505 //下落时间
#define LEVELUPMSG 	0x510 //升级消息
#define SCOREMSG 		0x515 //加分消息
#define GAMEOVERMSG 0x520 //GameOver消息


extern const unsigned teris[7][4][4][4];

//定义颜色
#define COLOR_BLACK   (Uint32)0x000000
#define COLOR_WHITE   (Uint32)0xffffff
#define COLOR_GRAY    (Uint32)0xa0a0a0
#define COLOR_RED 		(Uint32)0xff1A00
#define COLOR_ORANGE 	(Uint32)0xff7e00
#define COLOR_YELLOW 	(Uint32)0xffdd00
#define COLOR_GREEN 	(Uint32)0x33ff33
#define COLOR_CYAN 		(Uint32)0x55f0f0
#define COLOR_BLUE 		(Uint32)0x3070d0
#define COLOR_PURPLE  (Uint32)0xaa30d0

typedef struct{
	Uint8 r;
	Uint8 g;
	Uint8 b;
} RGB_COLOR;

const RGB_COLOR teris_color[7] = { 
	//这是方块的颜色
	{0xff, 0x1a, 0x00}, //COLOR_RED,
	{0xff, 0x7e, 0x00}, //COLOR_ORANGE,
	{0xff, 0xdd, 0x00}, //COLOR_YELLOW,
	{0x33, 0xff, 0x33}, //COLOR_GREEN,
	{0x55, 0xf0, 0xf0}, //COLOR_CYAN,
	{0x30, 0x70, 0xd0}, //COLOR_BLUE,
	{0xaa, 0x30, 0xd0}, //COLOR_PURPLE
};

const RGB_COLOR C_WHITE = {0xff, 0xff, 0xff};
const RGB_COLOR C_BLACK = {0x00, 0x00, 0x00};
const RGB_COLOR C_GRAY  = {0xa0, 0xa0, 0xa0};

const Uint32 clean_score[4] = {
	10,
	20,
	40,
	80
};

/*
const unsigned g_downtime[11] = { 
	10, //这个来存放最小的游戏处理粒度
	3000, //3
	2500, //2.5
	2000, //2
	1500, //1.5
	1000, //1
	750,  //3/4
	500,  //1/2
	250,  //1/4
	100,  //1/10
	50   //1/20
}; */

SDL_TimerID idtimer_fall;
const unsigned LOOPTIME = 1000 / 30; //循环时间
const unsigned DOWNTIME = 45;
const unsigned MAXLEVEL = 20;
const unsigned LEVELRISE = 30;
const unsigned MAP_X = 15;
const unsigned MAP_Y = 2;

class Global
{
	public:
		Global()
		{
			//初始化公有变量
			Screen = NULL;
			fg = NULL;
			curt_teris = NULL;
			next_teris = NULL;
			score = 0;
			rise = 0;
			level = 0;
			//map.Init_map();
			map = NULL;
		}
		~Global()
		{
			//干掉公有变量
		}
	public: //初始化
		SDL_Surface *InitScreen();

	private: //绘制
		unsigned PrintScreen();
		unsigned PrintBg(); //这个函数用来绘制背景
		unsigned PrintFg(); //这个函数用来绘制前景
		unsigned PrintMg();
		unsigned printRise();
		bool LoadBG(const char *bgfile);

	public:
		unsigned title_game(); 	//这个函数主要是游戏的标题
				//返回了一个整形变量表示玩家的选择.
		bool     game_over();   //游戏结婚结束之后的动画

	public:
		unsigned start_game(); //主要游戏循环
		CTeris* get_teris(); 	 //得到一个新的方块
		bool PutTerisToMap(); //把方块画到地图上
		bool CleanMap();      //消除已经填满一行的方块
		bool CrashMap();      //消除的一行上面的方块落下
		bool IsNoGameOver();  	//测试游戏是否gameover

	public: //消息
		unsigned MessageLoop();
		bool SeedMSG(Uint32 interval);  //发消息的函数
	private:
		unsigned Message_handle(SDL_Event* event);

	public:
		SDL_Surface *Screen; //屏幕窗口
		SDL_Surface *fg;     //
		CTeris 	*curt_teris; //当前活动的方块
		CTeris  *next_teris; //下一个准备好的方块  
		unsigned *map; //地图
		unsigned level; //游戏难度
		unsigned rise; //层数
		unsigned score; //游戏分数
		Uint32 	act_color; //角色颜色
		Uint32  map_color; //地图背景颜色
		SDL_UserEvent userevent; 

};

//到时间就往消息循环发消息
Uint32 handle_timer(Uint32 interval, void* param)
{
	//SDL_Event event;
	//SDL_UserEvent userevent;
	Global *_global = (Global*)param;

	_global->userevent.type = SDL_USEREVENT;
	_global->userevent.code = TIMEMSG; //这里是自定义的
	_global->userevent.data1 = NULL;
	_global->userevent.data2 = NULL;

	//event.type = SDL_USEREVENT;
	//event.user = userevent;

	SDL_PushEvent((SDL_Event*)&(_global->userevent));
	return(interval);
}


//初始化主界面
SDL_Surface *Global::InitScreen()
{
	if( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 ) {
		//初始化失败
#ifdef __TEXT_DEBUG
		std::cout << "初始化失败:" << SDL_GetError() << std::endl;
#endif
		return NULL;
	}
	
	//初始化屏幕
	Screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, SCREEN_COLOR, SDL_SWSURFACE|SDL_DOUBLEBUF);
	if( !Screen )
		return NULL;

	SDL_WM_SetCaption( SCREEN_TITLE ,NULL);

	return Screen ? Screen : NULL;
}


//绘制函数
unsigned Global::PrintScreen()
{
	if(!Screen)
		return 1;
	PrintBg(); //绘制背景图片
	PrintMg();
	printRise();
	//PrintMessage();
	PrintFg();
	SDL_Flip(Screen);
	return 0;
}

unsigned Global::printRise()
{
	SDL_Rect rect;
	int w = SCREEN_X/32;
	int h = SCREEN_Y/24;
	int X = 3 	* w; //next的X顶点坐标位置
	int Y = 18 	* h; //next的Y顶点坐标位置
	unsigned *nTeris = NULL;
	Uint32 colorbg = 0xf445b0ff;
	Uint32 colorfont = 0xffffffff;
	char szString[11] = {0};
	int ret = 0;
	SDL_Surface* bg = SDL_GetVideoSurface();
	if(!bg){
		return 0;
	}

	rect.x = w * 3; 
	rect.y = h * 3;
	rect.w = w * 10;
	rect.h = h * 3;
	
	sprintf(szString, "%010d\0", rise); //转换层数
	stringColor(bg, (rect.x),(rect.y), "Rise:\0", colorfont);//	colorfont);
	stringColor(bg, (rect.x), (rect.y+h), szString, colorbg);
	sprintf(szString, "%010d\0", score); //转换分数
	stringColor(bg, (rect.x),(rect.y+4*h), "Score:\0", colorfont);
	stringColor(bg, (rect.x),(rect.y+5*h), szString, colorbg);
	sprintf(szString, "%010d\0", level); //转换级别
	stringColor(bg, (rect.x),(rect.y+8*h), "Level:\0", colorfont);
	stringColor(bg, (rect.x),(rect.y+9*h), szString, colorbg);
	
	rect.y +=  11 * h;
	rect.w =  w* 4;
	rect.h =  h* 4;

	stringColor(bg, (rect.x), (rect.y), "Next:\0", colorfont);
	//rectangleColor(bg, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, colorbg );

	if(next_teris)	{ //下一个方块
		if(nTeris = next_teris->Get_teris()){
			for(int i = 0; i < 4; i++){
				for(int j = 0; j < 4; j++){
					int teris_point = *(nTeris + (i * 4) + j);
					if(teris_point ){ //是否应该画在地图上
						int x = j * w + X;
						int y = i * h + Y;

						boxRGBA(bg, x, y, x + w, y + h,
							teris_color[next_teris->m_type].r, 
							teris_color[next_teris->m_type].g, 
							teris_color[next_teris->m_type].b, 0xff);
						rectangleRGBA(bg, x, y, x + w, y + h, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);

					}
				}
			}
		}
	}
	return 1;
}

unsigned Global::PrintFg()
{
	int w = SCREEN_X/32;
	int h = SCREEN_Y/24;
	int x = 0, y = 0;
	Uint32 color ;
	SDL_Surface* bg = SDL_GetVideoSurface();
	unsigned* nTeris = NULL;
	//unsigned* nMap = NULL;
	SDL_Rect rect;
	rect.w = w;
	rect.h = h;

	
	if(!bg && !curt_teris)
		return 0;
	
	color = SDL_MapRGB(bg->format, 255, 0, 255);

	
	if(SDL_MUSTLOCK(bg))
		SDL_LockSurface(bg);
	
	nTeris = curt_teris->Get_teris();
	if(nTeris && map) {
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				//int teris_point = *(nTeris + i*4 + j); 
				int teris_point = *(nTeris + (i * 4) + j);
		//		int tmp = teris[curt_teris->m_type][curt_teris->m_faceto][i][j];
				int map_point = *(map + (curt_teris->get_Y() + i )*_MAP_WIDTH + (curt_teris->get_X() + j));
				if(teris_point ){ //是否应该画在地图上
					rect.x = (curt_teris->get_X()+j + MAP_X) * w;
					rect.y = (curt_teris->get_Y()+i + MAP_Y) * h;

					boxRGBA(bg, rect.x, rect.y ,rect.x + rect.w, rect.y + rect.h,
						teris_color[curt_teris->m_type].r, 
						teris_color[curt_teris->m_type].g, 
						teris_color[curt_teris->m_type].b, 0xff);
					//rectangleRGBA(bg, x, y, x + w, y + h, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);
					rectangleRGBA(bg, rect.x, rect.y ,rect.x + rect.w, rect.y + rect.h, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);
				}
			} 
		}
	}

	if(SDL_MUSTLOCK(bg))
		SDL_LockSurface(bg);
	
	return 1;
}

unsigned Global::PrintBg()
{
	//把背景刷黑
	SDL_Surface* bg = SDL_GetVideoSurface();
	SDL_Rect rect;
	rect.x = 0;
	rect.y = 0;
	rect.w = SCREEN_X;
	rect.h = SCREEN_Y;
//	Uint32 color;

	if( !bg )
		return 0 ;

	if(SDL_MUSTLOCK(bg))
		SDL_LockSurface(bg);

	Uint32 color = SDL_MapRGB(bg->format, C_BLACK.r, C_BLACK.g, C_BLACK.b);
	SDL_FillRect (bg, &rect, color);
	
	
  if(SDL_MUSTLOCK(bg))
		SDL_UnlockSurface(bg);

	return 0;
}

unsigned Global::PrintMg()
{
	SDL_Surface* bg = SDL_GetVideoSurface();
	int w = SCREEN_X/32;
	int h = SCREEN_Y/24;
	SDL_Rect rect;
	rect.x = MAP_X * w;
	rect.y = MAP_Y * h;
	rect.w = _MAP_WIDTH * w;
	rect.h = _MAP_HIGHT * h;

	if(!bg)
		return 0;

//	Uint32 color_fg = SDL_MapRGB(bg->format, 200,200,200);
//	Uint32 color_bg = SDL_MapRGB(bg->format, 50,50,50);
//	Uint32 color_mg = SDL_MapRGB(bg->format, 0,200,40);
	
	if(SDL_MUSTLOCK(bg))
		SDL_LockSurface(bg);

	SDL_FillRect(bg, &rect, COLOR_BLUE); //画背景

	for(int y = 0; y < _MAP_HIGHT -2; y++)
		for(int x = 2; x < _MAP_WIDTH -2; x++)
			if(*map == _FULL) { //这里开始画已经静止的方块
				SDL_Rect blackrect;	
				blackrect.x = rect.x + x * w; 
				blackrect.y = rect.y + y * h;
				blackrect.w = w;
				blackrect.h = h;
				if( *(map + y * _MAP_WIDTH + x) == 1 ){
					boxRGBA(bg, blackrect.x, blackrect.y ,blackrect.x + blackrect.w, blackrect.y + blackrect.h, C_GRAY.r, C_GRAY.g, C_GRAY.b, 0xff);
					rectangleRGBA(bg, blackrect.x, blackrect.y ,blackrect.x + blackrect.w, blackrect.y + blackrect.h, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);
				}
				else{
					Uint32 color = SDL_MapRGB(bg->format, C_BLACK.r, C_BLACK.g, C_BLACK.b);
					//SDL_FillRect(bg, &blackrect, color);
					boxRGBA(bg, blackrect.x, blackrect.y ,blackrect.x + blackrect.w, blackrect.y + blackrect.h, C_BLACK.r, C_BLACK.g, C_BLACK.b, 0xff);
					//rectangleRGBA(bg, blackrect.x, blackrect.y ,blackrect.x + blackrect.w, blackrect.y + blackrect.h, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);
				}
			}
	if(SDL_MUSTLOCK(bg))
		SDL_LockSurface(bg);

	return 1;
	
}

bool Global::LoadBG(const char *bgfile)
{
	fg = SDL_LoadBMP(bgfile);
	return fg? true: false;
}


//这个函数是主消息循环,负责处理程序中的输入和消息
//参数是个全局变量
unsigned Global::MessageLoop()
{
	SDL_Event event ; //消息变量
	unsigned ret = 1;
	Uint32 starttime;
	Uint32 endtime;
	while(ret){
		//消息处理函数
		starttime = SDL_GetTicks();
		//SDL_PollEvent(&event);
		SDL_WaitEvent(&event);
		ret = Message_handle(&event);

		endtime = SDL_GetTicks() - starttime;
		if(endtime < LOOPTIME)
			SDL_Delay(LOOPTIME - endtime);
		 
		//这里开始绘制
		PrintScreen();
	}
	return ret;
}

bool Global::SeedMSG(Uint32 interval)  //发消息的函数
{
	int ret = 0;
	userevent.type = interval;
	userevent.code = TIMEMSG; //这里是自定义的
	userevent.data1 = NULL;
	userevent.data2 = NULL;
	ret = SDL_PushEvent((SDL_Event*)&(userevent));
	return ret == 0 ? true: false;
}


//被主消息循环调用,对消息做出响应, 同时这个函数也负责窗口的绘制
unsigned Global::Message_handle(SDL_Event* event)
{
	SDL_Event quit_ent;
	SDL_KeyboardEvent *kb_ent = NULL;
	if(!event) {
		return false;
	}
	quit_ent.type = SDL_QUIT;

	switch(event->type){ //消息处理
		case SDL_QUIT:
			return 0;
			break;
		case SDL_KEYDOWN:
			{
				kb_ent = (SDL_KeyboardEvent*)event;
				switch( kb_ent->keysym.sym )
				{
					case SDLK_UP: //按下箭头上旋转方块
						{
#ifdef __TEXT_DEBUG
							std::cout << "pass up" << std::endl;
#endif
							if(curt_teris) {
								curt_teris->turn_left((unsigned*)map);
							}
						}break;
					case SDLK_DOWN: //按下箭头下
						{
#ifdef __TEXT_DEBUG
							std::cout << "pass down" << std::endl;
#endif
							if(curt_teris) {
								curt_teris->go_down((unsigned*)map);
							}
						}break;
					case SDLK_LEFT:
						{
#ifdef __TEXT_DEBUG
							std::cout << "pass left" << std::endl;
#endif
							if(curt_teris){
								curt_teris->go_left((unsigned*)map);
							}	
						}break;
					case SDLK_RIGHT:
						{
#ifdef __TEXT_DEBUG
							std::cout << "pass right" << std::endl;
#endif
							if(curt_teris) {
								curt_teris->go_right((unsigned*)map);
							}
						}break;
				}
			}break;
		case SDL_KEYUP:
			{
				kb_ent = (SDL_KeyboardEvent*)event;
				if( kb_ent->keysym.sym == SDLK_ESCAPE ) {
					SDL_PushEvent(&quit_ent); //发送一个退出消息
#ifdef __TEXT_DEBUG
					cout << "发出退出命令" << endl;
#endif
				}
			}break;
			case SDL_USEREVENT:
			{
				//进入判断流程
				if(((SDL_UserEvent*)event)->code == TIMEMSG) { //如果自定义消息为 0 就处理下落
					if(curt_teris && curt_teris->go_down((unsigned*)map)) {
#ifdef __TEXT_DEBUG
						cout << "下落" << endl;
#endif
					}else{
						PutTerisToMap();
						CleanMap();
						CrashMap();
						if (get_teris() == NULL){
#ifdef __TEXT_DEBUG
							cout << "GameOver" << endl;
#endif
							SDL_PushEvent(&quit_ent);
						}else{
#ifdef __TEXT_DEBUG
							cout << "创建了一个新方块" << endl;
#endif
						}
					}
				}
			}break;
		//case SDL_MOUSEBUTTONDOWN:
		//	SDL_PushEvent(&quit_ent);
		//	break;
	}

	//绘制窗口
	PrintScreen();
	return 1;
}

/*
 * 	从这里开始是游戏逻辑代码
 */

CTeris* Global::get_teris()
{
	if(next_teris){
		delete curt_teris;
		curt_teris = next_teris;
		next_teris = new CTeris();
	}else{
		curt_teris = new CTeris();
		next_teris = new CTeris();
	}
	
	if(!IsNoGameOver()){
		return NULL;
	}
	
	return curt_teris; 
}


bool Global::IsNoGameOver()
{
	bool bRet = true;
	unsigned tx, ty; //方块在map上面的坐标
	unsigned act_x, act_y;
	unsigned X,Y;
	unsigned *pT = NULL;
	unsigned teris_point = NULL, map_point = NULL;

	if(!curt_teris ||!map)
		return false;

	tx = curt_teris->get_X();
	ty = curt_teris->get_Y();

	for(act_y = 0; act_y < 4; act_y++)
		for(act_x = 0; act_x <4; act_x++){
			X = tx + act_x;
			Y = ty + act_y;
			pT = (unsigned*)curt_teris->Get_teris();
			teris_point = *(map + Y* _MAP_WIDTH +X);
			if(teris_point && map_point) {
				bRet = false; // 如果初始方块无法放入地图,说明game over
			}
		}
	return bRet;
}

bool Global::game_over()
{
	//暂时就是在屏幕中间显示game over的字样
	SDL_Surface* bg = Screen;
	char szMessage[] ={"Game Over! Hit Enter exit or any key again."};
	int ret = 0;
	if(!bg)
		return false;
	
	SDL_Rect rect;
	SDL_Rect blackrect;

	rect.w = bg->w / 32;
	rect.h = bg->h / 24;
	rect.x = bg->w / 2 - 40;
	rect.y = bg->h / 2 - 30;


	blackrect.x = 0;
	blackrect.y = 0;
	blackrect.w = bg->w;
	blackrect.h = bg->h;

	//Uint32 color = SDL_MapRGB(bg->format, C_BLACK.r, C_BLACK.g, C_BLACK.b);
	//SDL_FillRect(bg, &blackrect, color);

	//stringRGBA(Screen, 60, 60, szMessage, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);

	while(true){
		Uint32 color = SDL_MapRGB(bg->format, C_BLACK.r, C_BLACK.g, C_BLACK.b);
		ret = SDL_FillRect(bg, &blackrect, color);
		if(ret == -1)
			std::cout << "false" << std::endl;
		stringRGBA(Screen, 60, 60, szMessage, C_WHITE.r, C_WHITE.g, C_WHITE.b, 0xff);
		SDL_Event event;
		SDL_WaitEvent(&event);
		if(!&event)
			continue;
		switch(event.type){
			case SDL_KEYDOWN:
				if(((SDL_KeyboardEvent*)&event)->keysym.sym == SDLK_ESCAPE)
				{
					return false;
				}else{
					return true;
				}
				break;
		}
	}
	return false;
}

unsigned Global::title_game() 	//这个函数主要是游戏的标题
{
	SDL_Rect blackrect;
	//屏幕中间画一个框
	if(!Screen){
		return 1;
	}
  char he[] = {"Welcome play my TERIS!"};	
  	blackrect.x = 0;
	blackrect.y = 0;
	blackrect.w = Screen->w;
	blackrect.h = Screen->h;
	SDL_FillRect(Screen, &blackrect, COLOR_BLACK);

	//显示文字
	Uint32 colorYellow = COLOR_YELLOW;
	Uint32 colorOrange = COLOR_ORANGE;
	stringRGBA(Screen, 20, 20, he, 0xf4,0x35,0x35,255);
	stringRGBA(Screen, 60, 60, "hit ANY key to play!", 0xff,0xff,0xff,0xff);

	SDL_Flip(Screen);
	while(true){	
		SDL_Event event;
		
		SDL_WaitEvent(&event);
		if(!&event)
			continue;
		switch(event.type){
			case SDL_KEYDOWN:
				if(((SDL_KeyboardEvent*)&event)->keysym.sym == SDLK_ESCAPE)
				{
					return 0;
				}else{
					//start_game();
					return 1;
				}
				break;
		}
	}
	return 0;
}

bool Global::PutTerisToMap()
{
	unsigned tx, ty; //方块原点在map上的坐标
	//unsigned x, y;   
	unsigned act_x, act_y; //方块上的坐标
	unsigned X,Y; 					//映射在地图上的坐标
	unsigned *pT;           //代表方块的原点指针
	unsigned teris_point, map_point; //实际上方块点的数值以及地图上点的数值

	if(!curt_teris ||!map)
		return false;

	tx = curt_teris->get_X(); 	
	ty = curt_teris->get_Y(); 
								
	for(act_y = 0; act_y < 4; act_y++){
		for( act_x = 0; act_x < 4; act_x++)
		{
			X = tx + act_x; //X,Y是地图上的地址
			Y = ty + act_y;
			pT = (unsigned*)curt_teris->Get_teris();
			teris_point = *(pT + act_y*4 + act_x); //m 代表方块cx,cy处的值
			map_point = *(map + Y* _MAP_WIDTH + X);
			*(map + Y* _MAP_WIDTH + X) = teris_point || map_point ; //这里把方块画到地图里面 
		}
	}

	return true;
}

//这个函数主要用来测试map中已经填满的行,并且消除他们,在消除行上面的方块下落
bool Global::CleanMap()
{
 	int X,Y;
	int SumX = 0;
	int SumClear = 0;
	
	if(!map)
		return false;

	for(Y = 0; Y < _MAP_HIGHT - 2 ; Y++){
		for(X = 2; X < _MAP_WIDTH - 2 ; X++) {
			SumX += *(map + Y*_MAP_WIDTH + X);
		}

		if(SumX == 10) {//判断是否可以消除
			for(X = 2 ; X < _MAP_WIDTH ; X++){
				*(map + Y*_MAP_WIDTH + X) = 0; //消除
			} //结束消除
			SumClear ++ ;
		} //结束判断
		SumX = 0;
	}//结束循环
	
	//加分代码
	rise += SumClear % 4;
	score += clean_score[SumClear % 4];
	//发送加分的消息
	
	if(rise >= level * LEVELRISE){
		level ++;
		//发送升级消息
	}

	//消除动画
	//
	return true;
}

bool Global::CrashMap()
{
	unsigned empty[_MAP_HIGHT-2] = {0}; // 1,代表有东西 0,代表没有
	int cur_y = 0; //当前所在行的指针
	int end_y = 0;
	int sum_x = 0;	
	if(!map)	
		return false;

	//检索一下看看哪一行是空行
	for(int y = _MAP_HIGHT - 3; y >= 0 ; y--){
		for(int x = 2; x < _MAP_WIDTH - 2 ; x++){
			sum_x += *(map + y * _MAP_WIDTH + x);
		}
		empty[y] = sum_x > 0 ? 1 : 0;
	 	sum_x = 0;
	}

	cur_y = _MAP_HIGHT - 2;
	
	do{
		end_y = -- cur_y ;
		if(empty[cur_y] == 0){ //如果当前这行为0
			while(empty[end_y] == 0 && end_y >= 0 ){
				end_y --;
			}

			if( end_y >= 0 ){ //找到了交换
				empty[cur_y] = empty[end_y];
				empty[end_y] = 0;
				for(int x = 2; x < _MAP_WIDTH -2 ; x++){
					*(map + cur_y * _MAP_WIDTH + x) = *(map + end_y * _MAP_WIDTH +x);
					*(map + end_y * _MAP_WIDTH + x) = 0;
				}
			}
		}
	}while( cur_y >= 0 );
	
	return true;
}

unsigned Global::start_game()
{
	bool bExit = true;
	unsigned bError = 0;
	unsigned time = 0;
 	score = 0; 
	rise = 0;
	level = 0;
	map = ClearMap(map);

	CTeris *teris = NULL;
	do{
		get_teris(); //得到一个方块
		
		if(!curt_teris){
			bError = true;
			break;
		}
		
		//这里设置下落时间
		time = (MAXLEVEL - level) * DOWNTIME;
	 	idtimer_fall = SDL_AddTimer(time, handle_timer, this);

		while(bExit){ //如果bExit假就跳出
			bExit = MessageLoop(); 
		}

	}while(bExit);
	
	if(map)
		delete map;

	map = NULL;

	return (unsigned)bExit;
}
//#ifdef USE_WIN32
//int _tmain(int argc, char* argv[])
//#else
int main(int argc, char* argv[])
//#endif
{
	//初始化
	Global global;
	global.InitScreen();

	if(!global.Screen) {
#ifdef __TEXT_DEBUG
		cout << "无法设置分辨率240x320:" << SDL_GetError() << endl;
#endif
 	} else {
#ifdef __TEXT_DEBUG
		cout << "初始化成功!" << endl;
#endif
		atexit(SDL_Quit);
	}

	//进入标题
	while(global.title_game() == 1){
		if( global.start_game() == 0)
			if(global.game_over() == 0)
				break;
	}
	exit(0);
	return 0;
}


